// GlRender.cpp: implementation of the GlRender class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GlRender.h"
#include "Point.h"
#include <math.h>
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
float L= 56.05;//176/3.14;
//#define L 56.05
float v1=0;
float v2=50;
Point* p1;
Point* p2;
float timer2=2;
float timer1=0;
float lastX1=0;
float lastY1=0;
float lastX2=L;
float lastY2=0;
float flag=0;
float cou=0;
//clock_t timer;
float _SystemTimer =0;
float pa1[10] ={1,2,3,4,5,6,7,8,9,10};
float pb2[10] ={0,2,3,4,5,6,7,8,9,10};
//TIMERPROC timerSystem(HWND   hwnd, UINT  uMsg,UINT  idEvent,DWORD  dwTime)      
//{      
// //printf("hello!\n");    
// 	::InvalidateRect(hwnd,NULL,FALSE);
//	::UpdateWindow(hwnd);
//}  
GlRender::GlRender()
{
	m_dc=NULL;
}

GlRender::~GlRender()
{
	//glDeleteLists(1,1);
}
void GlRender::setFlag()
{
	flag =1;
}
void GlRender::Init(HDC dc)
{
	m_dc=dc;
	Clear();

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glOrtho(-100,200,-100,200,1,200);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
	glShadeModel(GL_SMOOTH);
	gluLookAt(0,0,20,0,0,0,0,1,0);

	Swap();

	
}
void GlRender::InitTimer()
{
	p1->timer = 0;
	p1->v=0;
	p2->timer = 10;
	p2->v=0;
	//timer=clock();
	
}

void GlRender::Run()
{
	int ic=0;
	int id=0;
	while(_SystemTimer<2000)
	{
			
		if(p1->timer==_SystemTimer)
		{
			CountPosition2();
			if(ic<9)
				p1->timer = pa1[ic++];
		}
		if(p2->timer==_SystemTimer)
		{
			CountPosition();
			if(id<9)
			p2->timer = pb2[id++];

			
		}
		_SystemTimer++;
	}
/*	    MSG   msg;        
    int   send = SetTimer(NULL, 0, 1000, timerSystem);       
    GetMessage   (&msg,   NULL,   0,   0);          
    TranslateMessage(&msg);      
    DispatchMessage(&msg);           
    KillTimer(NULL, send); */     

}

void GlRender::CountPosition()
{
	//_SystemTimer++;
	//p2->setV(v2);
	//p2->setT(timer2);
	float delta = 0.1;//(p2->v)*(p2->timer)/L;
	//std::cout<<p2->v<<std::endl;
	//float a = (p2->y-p1->y)/L;
	cou = cou+delta;
	p2->setPointX(p1->x + L * cos(cou));
	p2->setPointY(p1->y + L * sin(cou));
	
	glColor3f(1.0,1.0,1.0);
		glBegin(GL_POINTS);
		glVertex3f(p2->getPointX(),p2->getPointY(),0);
	    glEnd();
	//lastX1 = p1->getPointX();
	//lastY1 = p1->getPointY();
	//lastX2 = p2->getPointX();//
	//lastY2 = p2->getPointY();
}
void GlRender::CountPosition2()
{

	//_SystemTimer++;
	//p1->setV(v2);
	//p1->setT(timer2);
	float delta = 0.1;//(p2->v)*(p2->timer)/L;
	//std::cout<<p2->v<<std::endl;
	//float a = (p2->y-p1->y)/L;
	cou = cou-delta;
	p1->setPointX(p2->x - L * cos(cou));
	p1->setPointY(p2->y - L * sin(cou));
	
	glColor3f(1.0,1.0,1.0);
		glBegin(GL_POINTS);
		glVertex3f(p1->getPointX(),p1->getPointY(),0);
	    glEnd();
	//lastX1 = p1->getPointX();
	//lastY1 = p1->getPointY();
	//lastX2 = p2->getPointX();
	//lastY2 = p2->getPointY();
}
void GlRender::RenderScene(float* a,float* b,float* c, float* d)
{
	Clear();
	if(!p1)
	{
		p2 = new Point(lastX2,lastY1,timer2,v2);
		p1 = new Point(lastX1,lastY1,timer1,v1);
		
	}
	*a = p2->x;
	*b = p2->y;
	*c = p1->x;
	*d = p1->y;
	Point* p3 = new Point(lastX1,lastY1,timer1,v1);
	Point* p4 = new Point(lastX2,lastY1,timer2,v2);
	glColor3f(1.0,0.0,0.0);
	glBegin(GL_LINE_STRIP);
		glVertex3f(125,0,0);
		glVertex3f(0,0,0);
	glEnd();
	glColor3f(0.0,1.0,0.0);
	glBegin(GL_LINE_STRIP);
		glVertex3f(0,0,0);
		glVertex3f(0,125,0);
	glEnd();
	glPointSize(3.0);
	//glColor3f(0.0,0.0,1.0);
		glBegin(GL_POINTS);
		//glColor3f(1,0,0);
		glVertex3f(p1->getPointX(),p1->getPointY(),0);
		glEnd();
		//glBegin(GL_POINTS);
		//glVertex3f(p4->getPointX(),p4->getPointY(),0);
		//glEnd();
		if(flag==0){}
		else
		{
		//int sub = 5;
		//while(sub-->0){
			//CountPosition();
			glBegin(GL_POINTS);
			glVertex3f(p2->getPointX(),p2->getPointY(),0);
			glEnd();
		}
	Swap();
	
	
}
void GlRender::RenderScene()
{
	Clear();
	if(!p1)
	{
		p2 = new Point(lastX2,lastY1,timer2,v2);
		p1 = new Point(lastX1,lastY1,timer1,v1);
	}
	Point* p3 = new Point(lastX1,lastY1,timer1,v1);
	Point* p4 = new Point(lastX2,lastY1,timer2,v2);
	glColor3f(1.0,0.0,0.0);
	glBegin(GL_LINE_STRIP);
		glVertex3f(125,0,0);
		glVertex3f(0,0,0);
	glEnd();
	glColor3f(0.0,1.0,0.0);
	glBegin(GL_LINE_STRIP);
		glVertex3f(0,0,0);
		glVertex3f(0,125,0);
	glEnd();
	glPointSize(3.0);
	glColor3f(0.0,0.0,1.0);
		glBegin(GL_POINTS);
		glColor3f(1,0,0);
		glVertex3f(p1->getPointX(),p1->getPointY(),0);
		glEnd();
		glBegin(GL_POINTS);
		glVertex3f(p2->getPointX(),p2->getPointY(),0);
		glEnd();
		if(flag==0){}
		else
		{
		////int sub = 5;
		////while(sub-->0){
		//	CountPosition();
		glColor3f(0.0,1.0,0.0);
			glBegin(GL_POINTS);
			glVertex3f(p2->getPointX(),p2->getPointY(),0);
			glEnd();
			
			//glBegin(GL_POINTS);
			//glVertex3f(p1->getPointX(),p1->getPointY(),0);
			//glEnd();
		}
	Swap();
	
}
void GlRender::ViewPort(int x, int y, int width, int height)
{
	glViewport(x,y,width,height);
	RenderScene();
}

void GlRender::Swap()
{
	SwapBuffers(m_dc);
}

void GlRender::Clear()
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}
